package com.devframe.component.ftlprocess.freemarker.context;
import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;



import org.apache.log4j.Logger;

import com.devframe.component.ftlprocess.FTLProcessGlobalConstants;
import com.devframe.component.ftlprocess.cache.MyFTLProcessCacheManager;
import com.devframe.component.ftlprocess.freemarker.directive.ModelDirective;
import com.devframe.component.ftlprocess.freemarker.directive.PageDirective;
import com.devframe.component.ftlprocess.freemarker.directive.RenderDirective;
import com.devframe.component.ftlprocess.freemarker.directive.ReportDirective;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.TemplateExceptionHandler;
/**
 * 模型解析工厂
 * @author  <b>詹保山</b>
 */
public abstract class MyFTLProcessHandleContext {
	private static Logger logger = Logger.getLogger(MyFTLProcessHandleContext.class);
    /**
     * 公共指令
     */
    private  Map<String,Object> directives=new HashMap<String, Object>();
    
	private Configuration reportConfiguration;
	
	private Configuration modelConfiguration;
	
	private Configuration renderConfiguration;
    /**
     * 模板加载的目录
     */
    private  File templateDirectory=null;
    
    private String templateFileRoot;
    /**
     * 
     */
    private Map<String,Object> customDirectiveMap;
    /**
     * 获取模板文件根路径
     * @return
     * @author:詹保山
     */
	public  String getTemplateFileRoot(){
		return this.templateFileRoot;
	}
	
	public abstract String accessTemplateFileRoot();
    
	private  File createTemplateDirectory () {
		if(templateDirectory==null){
			logger.info("读取模板的目录");
			// 获取设置的模板的根目录
			String tmplDir = getTemplateFileRoot();
			logger.info(tmplDir);
			templateDirectory = new File(tmplDir);
			logger.info("文件：" + templateDirectory.exists() + "..."+ templateDirectory.getAbsolutePath());
		}
		return templateDirectory;
	}
	/**
	 * 初始化模板预定义指令
	 * 
	 * @author:詹保山
	 */
	public  void createTemplateDirective(){
		if(this.customDirectiveMap!=null){
			this.directives.putAll(this.customDirectiveMap);
		}
		directives.put("render", new RenderDirective());
		directives.put("model", new ModelDirective());
		directives.put("report", new ReportDirective());
		directives.put("page", new PageDirective());
		
		
	}
	private  void createReportConfiguration() {
		if(reportConfiguration==null){
		    reportConfiguration = new Configuration();
			reportConfiguration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
			reportConfiguration.setObjectWrapper(new DefaultObjectWrapper());
			reportConfiguration.setClassicCompatible(true);
			reportConfiguration.setEncoding(Locale.getDefault(), "UTF-8");
			try {
				// 添加共享变量
				reportConfiguration.setSharedVariable(FTLProcessGlobalConstants.RESOLVE_SHARED_NAME,FTLProcessGlobalConstants.RESOLVE_REPORT_MARK);
				reportConfiguration.setDirectoryForTemplateLoading(this.createTemplateDirectory());
				// cfg.setSetting("NoCache", "false");
			} catch (Exception e) {
				logger.error("模板配置文件加载出现错误", e);
			}
		}
	}

	private void  createModelConfiguration() {
		if(modelConfiguration==null){
		    modelConfiguration = new Configuration();
			modelConfiguration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
			modelConfiguration.setObjectWrapper(new DefaultObjectWrapper());
			modelConfiguration.setClassicCompatible(true);
			modelConfiguration.setEncoding(Locale.getDefault(), "UTF-8");
			try {
				// 添加共享变量
				modelConfiguration.setSharedVariable(FTLProcessGlobalConstants.RESOLVE_SHARED_NAME,FTLProcessGlobalConstants.RESOLVE_MODEL_MARK);
				modelConfiguration.setDirectoryForTemplateLoading(this.createTemplateDirectory());
				// cfg.setSetting("NoCache", "false");
			} catch (Exception e) {
				logger.error("模板配置文件加载出现错误", e);
			}
		}
	}

	private void createRenderConfiguration() {
		if(renderConfiguration==null){
			renderConfiguration = new Configuration();
			renderConfiguration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
			renderConfiguration.setObjectWrapper(new DefaultObjectWrapper());
			renderConfiguration.setClassicCompatible(true);
			renderConfiguration.setEncoding(Locale.getDefault(), "UTF-8");
			try {
				// 添加共享变量
				renderConfiguration.setSharedVariable(FTLProcessGlobalConstants.RESOLVE_SHARED_NAME,FTLProcessGlobalConstants.RESOLVE_RENDER_MARK);
				renderConfiguration.setDirectoryForTemplateLoading(this.createTemplateDirectory());
				// cfg.setSetting("NoCache", "false");
			} catch (Exception e) {
				logger.error("模板配置文件加载出现错误", e);
			}
		}
	}
	
	public void init(){
		logger.info("初始化操作");
		this.createTemplateDirective();
		
		this.createReportConfiguration();
		
		this.createModelConfiguration();
		
		this.createRenderConfiguration();
	}
 
	
	public Map<String, Object> getDirectives() {
		return directives;
	}
	public Configuration getReportConfiguration() {
		return reportConfiguration;
	}
	public Configuration getModelConfiguration() {
		return modelConfiguration;
	}
	public Configuration getRenderConfiguration() {
		return renderConfiguration;
	}
	public File getTemplateDirectory() {
		return templateDirectory;
	}



	public void setTemplateFileRoot(String templateFileRoot) {
		this.templateFileRoot = templateFileRoot;
	}

	public Map<String, Object> getCustomDirectiveMap() {
		return customDirectiveMap;
	}

	public void setCustomDirectiveMap(Map<String, Object> customDirectiveMap) {
		this.customDirectiveMap = customDirectiveMap;
	}

}
